package com.mmlc.utils;

import android.widget.ImageView;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

/**
 * Created by Administrator on 2016/7/5.
 */
public class GlideUtils {

    public static void loadLocal(RequestManager manager, int localResId, ImageView iv) {
        manager.load(localResId).into(iv);
    }

    public static void load(RequestManager manager, String url, ImageView iv) {
        load(manager, url, iv, 0, 0, false, false);
    }

    public static void load(RequestManager manager, String url, ImageView iv, int placeResId, int errorResId) {
        load(manager, url, iv, placeResId, errorResId, false, false);
    }

    public static void load(RequestManager manager, String url, ImageView iv, boolean centerCrop, boolean fitCenter) {
        load(manager, url, iv, 0, 0, centerCrop, fitCenter);
    }

    public static void load(RequestManager manager, String url, ImageView iv,
                            int placeResId, int errorResId,
                            boolean centerCrop, boolean fitCenter) {
        load(manager, url, iv, null, placeResId, 0, errorResId, 0, true, centerCrop, fitCenter);

    }

    public static void load(RequestManager manager, String url, ImageView iv,
                            DiskCacheStrategy diskCacheStrategy,
                            int placeResId, int fallResId, int errorResId, int animationId,
                            boolean crossFade, boolean centerCrop, boolean fitCenter) {
        DrawableRequestBuilder<String> requestBuilder = manager.load(url)
                .diskCacheStrategy(diskCacheStrategy != null ? diskCacheStrategy : DiskCacheStrategy.ALL);

        if (placeResId != 0) {
            requestBuilder.placeholder(placeResId);
        }
        if (fallResId != 0) {
            requestBuilder.fallback(fallResId);
        }
        if (errorResId != 0) {
            requestBuilder.error(errorResId);
        }
        if (animationId != 0) {
            requestBuilder.animate(animationId);
        }
        if (crossFade) {
            requestBuilder.crossFade();
        }
        if (centerCrop) {
            requestBuilder.centerCrop();
        } else if (fitCenter) {
            requestBuilder.fitCenter();
        }

        requestBuilder.into(iv);
    }
}
